package com.xbts.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.system.domain.PartyPointsDeclarationStatistics;
import com.xbts.system.mapper.PartyPointsDeclarationStatisticsMapper;
import com.xbts.system.service.IPartyPointsDeclarationStatisticsService;
import org.springframework.stereotype.Service;

/**
 * 积分统计Service业务层处理
 *
 * @author xbts
 * @date 2023-06-15
 */
@Service
public class PartyPointsDeclarationStatisticsServiceImpl extends ServiceImpl<PartyPointsDeclarationStatisticsMapper, PartyPointsDeclarationStatistics>
        implements IPartyPointsDeclarationStatisticsService {

    /**
     * 新增积分统计
     *
     * @param partyPointsDeclarationStatistics 积分统计
     * @return 结果
     */
    @Override
    public boolean savePartyPointsDeclarationStatistics(PartyPointsDeclarationStatistics partyPointsDeclarationStatistics) {
        LambdaQueryWrapper<PartyPointsDeclarationStatistics> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PartyPointsDeclarationStatistics::getBelongToArea, partyPointsDeclarationStatistics.getBelongToArea());
        lqw.eq(PartyPointsDeclarationStatistics::getYear, partyPointsDeclarationStatistics.getYear());
        lqw.last("limit 1");
        PartyPointsDeclarationStatistics statistics = getOne(lqw);
        // 如果已经存在则更新
        if (null != statistics){
            double rewardPoints = statistics.getRewardPoints() + partyPointsDeclarationStatistics.getRewardPoints();
            partyPointsDeclarationStatistics.setRewardPoints(rewardPoints);
            return update(partyPointsDeclarationStatistics, lqw);
        } else {
            return save(partyPointsDeclarationStatistics);
        }
    }

}
